package com.asset.inventory.manager

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.asset.inventory.base.BaseApp
import com.asset.inventory.kt.convertHexToString
import com.asset.inventory.kt.logE
import com.asset.inventory.utils.StringUtils
import com.gg.reader.api.dal.GClient
import com.gg.reader.api.dal.HandlerTagEpcLog
import com.gg.reader.api.dal.HandlerTagEpcOver
import com.gg.reader.api.protocol.gx.*
import com.gg.reader.api.utils.HksPower
import java.lang.Exception
import java.util.*
import kotlin.math.log

/**
 * 页面名称：RFID 工具类
 * 创建时间  2021/04/06 20:08
 */
@Suppress("CAST_NEVER_SUCCEEDS")
class RfidManager private constructor() {

    private val TAG = "RfidManager"

    private var mGClient: GClient = GClient()

    val epcLiveData  = MutableLiveData<LogBaseEpcInfo>()



    companion object {
        val instance: RfidManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            RfidManager() }
    }
    private var lastReadDataTime = 0L
    /**
     * 打开串口
     */
    fun openSerial(){
        if(!BaseApp.instance.isLoaderRfidSdk){
            return
        }
        try {
            //打开串口
            if(mGClient.openCusAndroidSerial("/dev/ttysWK0:115200",64,100)){
                mGClient.onTagEpcLog = HandlerTagEpcLog{ readName: String, logBaseEpcInfo: LogBaseEpcInfo? ->
                    if(0 == logBaseEpcInfo?.result){
                        //if(System.currentTimeMillis() - lastReadDataTime > 100){
                            lastReadDataTime = System.currentTimeMillis()
                            "onTagEpcLog: ===>$logBaseEpcInfo".logE()
                            logBaseEpcInfo.epc = StringUtils.convertHexToString(logBaseEpcInfo.epc)
                            if (logBaseEpcInfo.epc.startsWith("K") || logBaseEpcInfo.epc.startsWith("Z")) {
                                epcLiveData.postValue(logBaseEpcInfo)
                            }
                        //}
                    }
                }

                mGClient.onTagEpcOver = HandlerTagEpcOver{ s: String, logBaseEpcOver: LogBaseEpcOver ->
                    "onTagEpcLog: ===> Epc log over".logE()
                }
                sendStopCmd()

            }else{
                "openCusAndroidSerial: ===> open faile".logE()
            }
        }catch (e: Exception){
            e.printStackTrace()
        }

    }

    /**
     * 发送停止指令
     */
    fun sendStopCmd(): Boolean{
        if(!BaseApp.instance.isLoaderRfidSdk){
            return false
        }
        try {
            val msgBaseStop = MsgBaseStop()
            mGClient.sendSynMsg(msgBaseStop)
            return if(msgBaseStop.rtCode.toInt() == 0){
                "sendStopRmd: ===> success".logE()
                true
            }else{
                "sendStopRmd: ===> error".logE()
                false
            }
        }catch (e: Exception){
            e.printStackTrace()
        }

        return false

    }

    /**
     * 功率配置
     */
    fun sendConfigCmd(): Boolean{
        if(!BaseApp.instance.isLoaderRfidSdk){
            return false
        }
        try {
            val msgBaseSetPower = MsgBaseSetPower()
            val hashtable = Hashtable<Int, Int>()
            hashtable[1] = 30
            msgBaseSetPower.dicPower = hashtable
            mGClient.sendSynMsg(msgBaseSetPower)
            return if(msgBaseSetPower.rtCode.toInt() == 0){
                "sendConfigCmd: ===> success".logE()
                true
            }else{
                "sendConfigCmd: ===> error".logE()
                false
            }
        }catch (e: Exception){
            e.printStackTrace()
        }
        return false

    }

    /**
     * 读取EPC数据区
     */
    fun sendReadEpcCmd(): Boolean{
        if(!BaseApp.instance.isLoaderRfidSdk){
            return false
        }
        try {
            val msgBaseInventoryEpc = MsgBaseInventoryEpc()
            msgBaseInventoryEpc.antennaEnable = EnumG.AntennaNo_1
            msgBaseInventoryEpc.inventoryMode = EnumG.InventoryMode_Inventory
            mGClient.sendSynMsg(msgBaseInventoryEpc)
            return if(msgBaseInventoryEpc.rtCode.toInt() == 0){
                "sendReadEpcCmd: ===> success".logE()
                true
            }else{
                "sendReadEpcCmd: ===> error".logE()
                false
            }
        }catch (e: Exception){
            e.printStackTrace()
        }
        return false

    }

    /**
     * 上下电
     * 0，下电； 1，上电
     */
    fun setPower(state: Int){
        if(!BaseApp.instance.isLoaderRfidSdk){
            return
        }
        try {
            HksPower.uhf_power(state)
        }catch (e: Exception){
            e.printStackTrace()
        }

    }

    /**
     * 关闭连接
     */
    fun close(){
        if(!BaseApp.instance.isLoaderRfidSdk){
            return
        }
        try {
            mGClient.close()
        }catch (e: Exception){
            e.printStackTrace()
        }

    }




}